package com.hd.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.base.service.BaseService;
import com.hd.enumerate.ResultCode;
import com.hd.util.ResultCommonUtils;


@Controller
public abstract class BaseController<T> {
	
	public abstract BaseService<T> getBaseService();


	@RequestMapping(value = "/save", 
			method = RequestMethod.POST,
			produces = MediaType.APPLICATION_JSON_VALUE + ";Charset=UTF-8")
	@ResponseBody
	public ResultCommonUtils saveModel(HttpServletRequest request, @RequestBody T model) {
		ResultCommonUtils resultCommon = new ResultCommonUtils();
		try {

			getBaseService().save(model);
			resultCommon.setCode(ResultCode.SUCCESS.getValue());
			resultCommon.setMessage("SUCCESS");
		} catch (Exception e) {
			resultCommon.setCode(ResultCode.INTERNAL_SERVER_ERROR.getValue());
			resultCommon.setMessage(e.getMessage());
		}
		return resultCommon;
	}

	@RequestMapping(value = "/update", 
			method = RequestMethod.PUT, 
			produces = MediaType.APPLICATION_JSON_VALUE + ";Charset=UTF-8")
	@ResponseBody
	public ResultCommonUtils updateModel(HttpServletRequest request, @RequestBody T model) {
		ResultCommonUtils resultCommon = new ResultCommonUtils();
		try {

			getBaseService().update(model);
			resultCommon.setCode(ResultCode.SUCCESS.getValue());
			resultCommon.setMessage("SUCCESS");
		} catch (Exception e) {
			resultCommon.setCode(ResultCode.INTERNAL_SERVER_ERROR.getValue());
			resultCommon.setMessage(e.getMessage());
		}
		return resultCommon;
	}

	@RequestMapping(value = "/delete",
			method = RequestMethod.DELETE, 
			produces = MediaType.APPLICATION_JSON_VALUE + ";Charset=UTF-8")
	@ResponseBody
	public ResultCommonUtils deleteModel(HttpServletRequest request, @RequestBody String id) {
		ResultCommonUtils resultCommon = new ResultCommonUtils();
		try {

			getBaseService().deleteById(id);
			resultCommon.setCode(ResultCode.SUCCESS.getValue());
			resultCommon.setMessage("SUCCESS");
		} catch (Exception e) {
			resultCommon.setCode(ResultCode.INTERNAL_SERVER_ERROR.getValue());
			resultCommon.setMessage(e.getMessage());
		}
		return resultCommon;
	}

	@RequestMapping(value = "/queryByList",
			method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE + ";Charset=UTF-8")
	@ResponseBody
	public ResultCommonUtils queryByList(HttpServletRequest request, @RequestBody T model) {
		ResultCommonUtils resultCommon = new ResultCommonUtils();
		try {

			List<T> listModel = getBaseService().queryByList();
			resultCommon.setData(listModel);
			resultCommon.setCode(ResultCode.SUCCESS.getValue());
			resultCommon.setMessage("SUCCESS");
		} catch (Exception e) {
			resultCommon.setCode(ResultCode.INTERNAL_SERVER_ERROR.getValue());
			resultCommon.setMessage(e.getMessage());
		}
		return resultCommon;
	}
}
